package com.saibo.spider.utils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 自定义转换器.将任意常用格式的时间字符串转换为Date对象.
 * 
 * 可处理的常见时间格式包括但不限于: <br/>
 * 1346038357441 (long) <br/>
 * 2012-08-27 11:32:37 2012-08-27 <br/>
 * 2012/08/27 11:32:37 2012/08/27 <br/>
 * 20120827113237 <br/>
 * 20120827 <br/>
 * Mon Aug 27 11:32:37 <br/>
 * CST 2012 (date.toString()) <br/>
 * 
 * @see cn.com.hzad.psm.utils.DateConverterTest
 * @author zhangzhening
 * 
 */
public class DateUtil {

	public static ThreadLocal<Map<String, DateFormat>> threadLocal = new ThreadLocal<Map<String, DateFormat>>();
	public static final String YYYYMMDDHHMMSSZ = "yyyyMMddHHmmssZ";
	public static final String YYYYMMDDHHMMSS_CN = "yyyy年MM月dd日 HH:mm:ss";
	public static final String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
	public static final String YYYYMMDDHHMMSSSSS = "yyyyMMddHHmmssSSS";
	public static final String YYYYMMDD = "yyyyMMdd";
	public static final String YYYYMMDD_CN = "yyyy年MM月dd日";
	public static final String YYYY_MM_DD = "yyyy-MM-dd";
	public static final String YYYY_M_D = "yyyy-M-d";
	public static final String YYYY1MM1DD = "yyyy/MM/dd";
	public static final String YYYY1M1D = "yyyy/M/d";
	public static final String YYYY_MM1DD = "yyyy-MM/dd";
	public static final String YYYY1MM1DD_HH_MM_SS = "yyyy/MM/dd HH:mm:ss";
	public static final String YYYY1M1D_HH_MM_SS = "yyyy/M/d HH:mm:ss";
	public static final String YYYY1MM1DD_HH_MM_SS_SSS = "yyyy/MM/dd HH:mm:ss.SSS";
	public static final String DD1MM1YYYY_HH_MM_SS_Z = "dd/MMM/yyyy:HH:mm:ss Z";
	public static final String YYYY_M_D_HH_MM_SS = "yyyy-M-d HH:mm:ss";
	public static final String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
	public static final String YYYY_MM_DD_HH_MM_SSZ = "yyyy-MM-dd HH:mm:ssZ";
	public static final String YYYY_MM_DD_HH_MM_SS_SSS = "yyyy-MM-dd HH:mm:ss.SSS";
	public static final String YYYY_MM_DD_HH_MM_SS_SSSZ = "yyyy-MM-dd HH:mm:ss.SSSZ";
	public static final String YYYY_MM_DD_KK_MM_SS_A = "yyyy-MM-dd KK:mm:ss a";
	public static final String YYYY_MM_DD_G_AT_HH_MM_SS_Z = "yyyy.MM.dd G 'at' hh:mm:ss z";
	public static final String EEE_MMM_DD_HH_MM_SS_ZZZ_YYYY = "EEE MMM dd hh:mm:ss zzz yyyy";

	/**
	 * 获取指定格式的DateFormat对象. 线程安全.
	 * 
	 * @param pattern
	 * @return
	 */
	public static DateFormat getDateFormat(String pattern) {
		Map<String, DateFormat> map = threadLocal.get();
		if (map == null) {
			map = new HashMap<String, DateFormat>();
			threadLocal.set(map);
		}
		DateFormat df = map.get(pattern);
		if (df == null) {
			if (EEE_MMM_DD_HH_MM_SS_ZZZ_YYYY.equals(pattern)) {
				df = new SimpleDateFormat(pattern, Locale.US);
			} else {
				df = new SimpleDateFormat(pattern);
			}
			map.put(pattern, df);
		}
		return df;
	}

	public static String dateToString(Date date) {
		if (date == null) {
			return "";
		}
		return format(YYYYMMDDHHMMSS_CN, date);
	}

	/**
	 * 使用指定格式格式化指定字符串. 线程安全.
	 * 
	 * @param pattern
	 * @param strDate
	 * @return
	 * @throws ParseException
	 */
	public static String format(String pattern, Date date) {
		return getDateFormat(pattern).format(date);
	}

	/**
	 * 返回当前时间,格式YYYY_MM_DD_HH_MM_SS
	 * 
	 * @return
	 */
	public static String getNewDateSrt() {
		return getDateFormat(YYYY_MM_DD_HH_MM_SS).format(new Date());
	}

	/**
	 * 使用指定格式解析指定字符串. 线程安全.
	 * 
	 * @param pattern
	 * @param strDate
	 * @return
	 * @throws ParseException
	 */
	public static Date parse(String pattern, String strDate) throws ParseException {
		return getDateFormat(pattern).parse(strDate);
	}

	/**
	 * 是否为同一天
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static boolean sameDate(Date date1, Date date2) {
		if (date1 == null || date2 == null) {
			return false;
		}
		DateFormat dateFormat = getDateFormat(YYYYMMDD);
		return dateFormat.format(date1).equals(dateFormat.format(date2));
	}

	/**
	 * 增加指定时间的一部分
	 * 
	 * @param date
	 *            基础时间
	 * @param year
	 *            年
	 * @param month
	 *            月
	 * @param dayOfMonth
	 *            日
	 * @param hourOfDay
	 *            时
	 * @param minute
	 *            分
	 * @param second
	 *            秒
	 * @param dayOfweek
	 *            周
	 * @return
	 */
	public static Date addDate(Date date, int year, int month, int dayOfMonth, int hourOfDay, int min, int second) {
		if (date == null) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, year);
		calendar.add(Calendar.MONTH, month);
		calendar.add(Calendar.DAY_OF_MONTH, dayOfMonth);
		calendar.add(Calendar.HOUR_OF_DAY, hourOfDay);
		calendar.add(Calendar.MINUTE, min);
		calendar.add(Calendar.SECOND, second);
		return calendar.getTime();
	}

	/**
	 * 设定指定时间的一部分
	 * 
	 * @param date
	 *            基础时间
	 * @param year
	 *            年
	 * @param month
	 *            月
	 * @param dayOfMonth
	 *            日 设定一个月的第几天,负数为倒数第几天(例如-2为当月倒数第二天) 只有dayOfMonth可以这样写
	 * @param hourOfDay
	 *            时
	 * @param minute
	 *            分
	 * @param second
	 *            秒
	 * @param dayOfweek
	 *            周
	 * @return
	 */
	public static Date setDate(Date date, Integer year, Integer month, Integer dayOfMonth, Integer hourOfDay,
			Integer minute, Integer second, Integer dayOfweek) {
		if (date == null) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		if (month != null) {
			calendar.set(Calendar.MONTH, month);
		}
		if (dayOfMonth != null) {
			if (dayOfMonth < 0) {
				int maximum = calendar.getMaximum(Calendar.DAY_OF_MONTH);
				dayOfMonth = maximum + 1 + dayOfMonth;
			}
			calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
		}
		if (hourOfDay != null) {
			calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
		}
		if (minute != null) {
			calendar.set(Calendar.MINUTE, minute);
		}
		if (second != null) {
			calendar.set(Calendar.SECOND, second);
		}
		if (dayOfweek != null) {
			calendar.set(Calendar.DAY_OF_WEEK, dayOfweek);
		}
		return calendar.getTime();
	}

	/**
	 * 万能时间转换. 对于不能识别的内容,返回null;
	 * 
	 * @param time
	 * @return
	 */
	public static Date stringToDate(final String time) {
		if (time == null || time.isEmpty()) {
			return null;
		}
		String timeTrim = time.trim();
		if (timeTrim.length() <= 1) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		if (timeTrim.contains("前") || timeTrim.contains("天")) {
			if (timeTrim.endsWith("前")) {
				return chineseTime(timeTrim);
			}
			if (timeTrim.startsWith("前天") || timeTrim.endsWith("前天")) {
				calendar.add(Calendar.DAY_OF_YEAR, -2);
				return calendar.getTime();
			} else if (timeTrim.startsWith("昨天") || timeTrim.endsWith("昨天")) {
				calendar.add(Calendar.DAY_OF_YEAR, -1);
				return calendar.getTime();
			} else if (timeTrim.startsWith("今天") || timeTrim.endsWith("今天")) {
				calendar.add(Calendar.DAY_OF_YEAR, 0);
				return calendar.getTime();
			}
		}
		int tempPos = time.indexOf("AD");
		String pattern = null;
		if (tempPos > -1) {
			timeTrim = timeTrim.substring(0, tempPos) + "公元" + timeTrim.substring(tempPos + "AD".length());
			pattern = YYYY_MM_DD_G_AT_HH_MM_SS_Z;
		}

		// 如果不包含数字,则不尝试继续判断时间格式
		if (!timeTrim.matches(".*[0-9].*")) {
			return calendar.getTime();
		}

		/*
		 * 当遇到没有年份与秒时使用 String[] dateTime = timeTrim.split(" ");
		 * 
		 * if (dateTime.length > 1 && dateTime.length < 3) { String[] year =
		 * dateTime[0].split("-"); if (year.length < 3) { Date date = new
		 * Date(); int mon = Integer.parseInt(year[0]); if (date.getMonth() >
		 * mon) { timeTrim = 20 + "" + (100 - date.getYear()) + "-" + timeTrim;
		 * } else { timeTrim = 20 + "" + ((date.getYear() - 101)) + "-" +
		 * timeTrim; } } String[] dateMin = dateTime[1].split(":"); if
		 * (dateMin.length < 3) { timeTrim += ":00"; } }
		 */

		int timeStrLength = timeTrim.length();
		boolean isInteger = isLong(timeTrim);
		boolean hasMinusSign = timeTrim.contains("-");
		boolean hasDivisionSign = timeTrim.contains("/");
		boolean hasBlankSpace = timeTrim.contains(" ");

		tempPos = timeTrim.indexOf("-");
		if (isInteger && timeStrLength == "1346038357441".length()) {
			Long lon = new Long(timeTrim);
			Date ctime = new Date(lon);
			return ctime;
		} else if (isInteger && timeStrLength == YYYYMMDDHHMMSSZ.length()) {
			pattern = YYYYMMDDHHMMSSZ;
		} else if (isInteger && timeStrLength == YYYYMMDDHHMMSS.length()) {
			pattern = YYYYMMDDHHMMSS;
		} else if (isInteger && timeStrLength == YYYYMMDD.length()) {
			pattern = YYYYMMDD;
		} else if (hasMinusSign && hasDivisionSign && timeStrLength == YYYY_MM1DD.length()) {
			pattern = YYYY_MM1DD;
		} else if (hasMinusSign && timeStrLength == YYYY_MM_DD.length()) {
			pattern = YYYY_MM_DD;
		} else if (hasMinusSign && timeStrLength >= YYYY_M_D.length() && timeStrLength < YYYY_MM_DD.length()) {
			pattern = YYYY_M_D;
		} else if (hasDivisionSign && timeStrLength == YYYY1MM1DD.length()) {
			pattern = YYYY1MM1DD;
		} else if (hasDivisionSign && timeStrLength >= YYYY1M1D.length() && timeStrLength < YYYY1MM1DD.length()) {
			pattern = YYYY1M1D;
		} else if (hasDivisionSign && hasBlankSpace && timeStrLength == YYYY1MM1DD_HH_MM_SS.length()) {
			pattern = YYYY1MM1DD_HH_MM_SS;
		} else if (hasBlankSpace && hasDivisionSign && timeStrLength >= YYYY1M1D_HH_MM_SS.length()
				&& timeStrLength < YYYY1M1D_HH_MM_SS.length()) {
			pattern = YYYY1M1D_HH_MM_SS;
		} else if (hasBlankSpace && hasMinusSign && timeStrLength == YYYY_MM_DD_HH_MM_SS.length()) {
			pattern = YYYY_MM_DD_HH_MM_SS;
		} else if (hasBlankSpace && hasMinusSign && timeStrLength >= YYYY_M_D_HH_MM_SS.length()
				&& timeStrLength < YYYY_MM_DD_HH_MM_SS.length()) {
			pattern = YYYY_M_D_HH_MM_SS;
		} else if (hasBlankSpace && hasDivisionSign && (timeTrim.indexOf("am") > -1)
				|| (timeTrim.indexOf("pm") > -1) && timeTrim.length() <= YYYY_MM_DD_KK_MM_SS_A.length() + 3) {
			pattern = YYYY_MM_DD_KK_MM_SS_A;
		} else if (hasBlankSpace && hasMinusSign && (timeTrim.indexOf("am") > -1)
				|| (timeTrim.indexOf("pm") > -1) && timeTrim.length() <= YYYY_MM_DD_KK_MM_SS_A.length() + 3) {
			pattern = YYYY_MM_DD_KK_MM_SS_A;
		} else if (hasBlankSpace && hasDivisionSign && timeStrLength == DD1MM1YYYY_HH_MM_SS_Z.length() + 4) {
			pattern = DD1MM1YYYY_HH_MM_SS_Z;
		} else if (hasBlankSpace && hasMinusSign && timeStrLength == YYYY_MM_DD_HH_MM_SSZ.length() + 4) {
			pattern = YYYY_MM_DD_HH_MM_SSZ;
		} else if (hasBlankSpace && hasMinusSign && timeStrLength == YYYY_MM_DD_HH_MM_SS_SSSZ.length() + 4) {
			pattern = YYYY_MM_DD_HH_MM_SS_SSSZ;
		} else if (timeStrLength == EEE_MMM_DD_HH_MM_SS_ZZZ_YYYY.length()) {
			pattern = EEE_MMM_DD_HH_MM_SS_ZZZ_YYYY;
		} else {
			Pattern p = Pattern.compile("([0-9]{2}-[0-9]{2})");
			Matcher m = p.matcher(timeTrim);
			if (m.find()) {
				timeTrim = dateToString(valiDate(time));
				pattern = YYYYMMDDHHMMSS_CN;
			}
		}
		Date ctime = null;
		if (pattern == null || timeTrim == null || timeTrim.isEmpty()) {
			return null;
		}
		try {
			ctime = parse(pattern, timeTrim);
		} catch (Exception e) {
		}
		return ctime;
	}

	/**
	 * 不定长时间识别
	 * 
	 * @param date
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static Date valiDate(String date) {
		if (date == null) {
			return null;
		}
		Date dateTime = new Date();

		// 标准格式YYYY-MM-DD HH:MM:SS
		String regex = ".*([0-9]{4}[-/][0-9]{1,2}[-/][0-9]{1,2}[\\s][0-9]{2}:[0-9]{2}:[0-9]{2}).*";
		String target = date.replaceAll(regex, "$1");
		if (target.matches(regex)) {
			return getDate(target);
		}
		// YY-MM-DD HH:MM:SS
		regex = ".*([0-9]{4}[-/][0-9]{1,2}[-/][0-9]{1,2}[\\s][0-9]{2}:[0-9]{2}).*";
		target = date.replaceAll(regex, "$1");
		if (target.matches(regex)) {
			target = target + ":00";
			return getDate(target);
		}

		// YY-MM-DD HH:MM:SS
		regex = ".*([0-9]{2}[-/][0-9]{1,2}[-/][0-9]{1,2}[\\s][0-9]{2}:[0-9]{2}:[0-9]{2}).*";
		target = date.replaceAll(regex, "$1");
		if (target.matches(regex)) {
			target = "20" + target;
			return getDate(target);
		}

		// MM-DD HH:MM
		regex = ".*([0-9]{2}[-/][0-9]{2}[\\s][0-9]{2}:[0-9]{2}).*";
		target = date.replaceAll(regex, "$1");

		if (target.matches(regex)) {
			String[] cache = target.split(" ");
			String[] year = cache[0].split("-");
			if (Integer.parseInt(year[0]) > (dateTime.getMonth() + 1)) {
				target = (1900 + dateTime.getYear() - 1) + "-" + target + ":00";
			} else if (Integer.parseInt(year[0]) == (dateTime.getMonth() + 1)) {
				if (Integer.parseInt(year[1]) > dateTime.getDate()) {
					target = (1900 + dateTime.getYear() - 1) + "-" + target + ":00";
				} else {
					target = (1900 + dateTime.getYear()) + "-" + target + ":00";
				}
			}
			return getDate(target);
		}

		// YYYY-MM-DD
		regex = ".*([0-9]{4}[-/][0-9]{2}[-/][0-9]{2}).*";
		target = date.replaceAll(regex, "$1");
		if (target.matches(regex)) {
			target += " 00:00:00";
			return getDate(target);
		}

		// YY-MM-DD
		regex = ".*([0-9]{2}[-/][0-9]{2}[-/][0-9]{2}).*";
		target = date.replaceAll(regex, "$1");
		if (target.matches(regex)) {
			target = "20" + target + " 00:00:00";
			return getDate(target);
		}

		// MM-DD
		regex = ".*([0-9]{2}[-/][0-9]{2}).*";
		target = date.replaceAll(regex, "$1");
		if (target.matches(regex)) {
			String[] year = target.split("-");
			if (Integer.parseInt(year[0]) > (dateTime.getMonth() + 1)) {
				target = (1900 + dateTime.getYear() - 1) + "-" + target + " 00:00:00";
			} else if (Integer.parseInt(year[0]) == (dateTime.getMonth() + 1)) {
				if (Integer.parseInt(year[1]) > dateTime.getDate()) {
					target = (1900 + dateTime.getYear() - 1) + "-" + target + " 00:00:00";
				} else {
					target = (1900 + dateTime.getYear()) + "-" + target + " 00:00:00";
				}
			} else if (Integer.parseInt(year[0]) < (dateTime.getMonth() + 1)) {
				target = (1900 + dateTime.getYear()) + "-" + target + " 00:00:00";
			}
			return getDate(target);
		}

		// HH:MM
		target = target.replaceAll(" : ", ":");
		regex = ".*([0-9]{2}:[0-9]{2}).*";
		target = date.replaceAll(regex, "$1");
		if (target.matches(regex)) {
			target = (1900 + dateTime.getYear()) + "-" + (dateTime.getMonth() + 1) + "-" + (dateTime.getDate()) + " "
					+ target + ":00";
			return getDate(target);
		}

		return null;
	}

	private static Date getDate(String receiptTimeCut) {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
		try {
			Date date = simpleDateFormat.parse(receiptTimeCut);
			return date;
		} catch (ParseException e1) {
			return null;
		}
		/*try {
			return getDateFormat(YYYY_MM_DD_HH_MM_SS).parse(receiptTimeCut);
		} catch (ParseException e) {
			return null;
		}*/
	}

	public static Date chineseTime(String timeTrim) {
		timeTrim = timeTrim.replaceAll("一", "1");
		timeTrim = timeTrim.replaceAll("两", "2");
		timeTrim = timeTrim.replaceAll("二", "2");
		timeTrim = timeTrim.replaceAll("三", "3");
		timeTrim = timeTrim.replaceAll("四", "4");
		timeTrim = timeTrim.replaceAll("五", "5");

		timeTrim = timeTrim.trim();
		timeTrim = timeTrim.replaceAll(" ", "");
		Calendar calendar = Calendar.getInstance();
		try {
			if (timeTrim.endsWith("月前")) {
				String intStr = timeTrim.replaceAll(".*([0-9]{1,2})个?月前.*", "$1");
				int i = Integer.parseInt(intStr);
				calendar.add(Calendar.MONTH, -i);
				calendar.set(Calendar.HOUR_OF_DAY, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
			} else if (timeTrim.endsWith("星期前")) {
				String intStr = timeTrim.replaceAll(".*([0-9]{1,2})个?星期前.*", "$1");
				int i = Integer.parseInt(intStr);
				calendar.add(Calendar.DAY_OF_YEAR, -i * 7);
				calendar.set(Calendar.HOUR, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
			} else if (timeTrim.endsWith("周前")) {
				String intStr = timeTrim.replaceAll(".*([0-9]{1,2})个?周前.*", "$1");
				int i = Integer.parseInt(intStr);
				calendar.add(Calendar.DAY_OF_YEAR, -i * 7);
				calendar.set(Calendar.HOUR, 0);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
			} else if (timeTrim.endsWith("天前")) {

				Pattern p = Pattern.compile("\\d{2}天前");
				Matcher m = p.matcher(timeTrim);
				if (m.matches()) {
					String intStr = timeTrim.replaceAll(".*([0-9]{2})天前.*", "$1");
					int i = Integer.parseInt(intStr);
					calendar.add(Calendar.DAY_OF_YEAR, -i);
					calendar.set(Calendar.HOUR, 0);
					calendar.set(Calendar.MINUTE, 0);
					calendar.set(Calendar.SECOND, 0);
				} else {
					String intStr = timeTrim.replaceAll(".*([0-9]{1})天前.*", "$1");
					int i = Integer.parseInt(intStr);
					calendar.add(Calendar.DAY_OF_YEAR, -i);
					calendar.set(Calendar.HOUR, 0);
					calendar.set(Calendar.MINUTE, 0);
					calendar.set(Calendar.SECOND, 0);
				}

			} else if (timeTrim.endsWith("小时前") && !timeTrim.endsWith("半小时前")) {
				String intStr = timeTrim.replaceAll(".*([0-9]{1,2})个?小时前.*", "$1");
				int i = Integer.parseInt(intStr);
				calendar.add(Calendar.HOUR, -i);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
			} else if (timeTrim.endsWith("分钟前")) {
				String intStr = timeTrim.replaceAll(".*([0-9]{1,3})分钟前.*", "$1");
				int i = Integer.parseInt(intStr);
				calendar.add(Calendar.MINUTE, -i);
				calendar.set(Calendar.SECOND, 0);
			} else if (timeTrim.endsWith("秒前")) {
				String intStr = timeTrim.replaceAll(".*([0-9]{1,3})秒前.*", "$1");
				int i = Integer.parseInt(intStr);
				calendar.add(Calendar.SECOND, -i);
			} else if (timeTrim.equals("半小时前")) {
				calendar.add(Calendar.HOUR, -1);
				calendar.set(Calendar.MINUTE, 0);
				calendar.set(Calendar.SECOND, 0);
			} else {
				return null;
			}
		} catch (Exception e) {
		}
		return calendar.getTime();
	}

	/** 获得指定时间的小时数 */
	public static int getHour(Date date) {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.HOUR_OF_DAY);
	}

	/** 获得指定时间的分钟数 */
	public static int getMinutes(Date date) {
		Calendar calendar = Calendar.getInstance();
		return calendar.get(Calendar.MINUTE);
	}

	/** 获得当前时间的小时数 */
	public static int getHour() {
		return getHour(new Date());
	}

	/** 获得当前时间的分钟数 */
	public static int getMinutes() {
		return getMinutes(new Date());
	}

	/**
	 * 检查是否是今天
	 * 
	 * @param date
	 * @return
	 */
	public static boolean isToday(Date date) {
		if (date == null) {
			return false;
		}
		DateFormat dateFormat = getDateFormat(YYYYMMDD);
		return dateFormat.format(new Date()).equals(dateFormat.format(date));
	}

	/**
	 * 判断字符串是否是整数.
	 */
	public static boolean isLong(final String value) {
		try {
			Long.parseLong(value);
			return true;
		} catch (Exception e) {
			return false;
		}
	}

	/***
	 * 今日时间获得到绝对时间
	 */
	public static Date shortDateTime(String dateTime) {
		dateTime = dateTime.trim();
		String[] time;
		if (dateTime.contains(":")) {
			time = dateTime.split(":");
		} else if (dateTime.contains("-")) {
			time = dateTime.split("-");
		} else {
			return new Date();
		}

		int hor = Integer.parseInt(time[0].replaceAll(" ", ""));
		int min = Integer.parseInt(time[1].replaceAll(" ", ""));
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, hor);
		calendar.set(Calendar.MINUTE, min);
		return calendar.getTime();
	}
	public static String getCurrDateTime(int num){
		Date date = new Date();// 取时间
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, num);// 把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return df.parse(df.format(date)).getTime()+"";
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static long getCurrDateTime() {
		Calendar c = Calendar.getInstance();
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		try {
			return df.parse(df.format(c.getTime())).getTime();
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0;
	}

	
	public static String getMissionDateTime(int num){
		Date date = new Date();// 取时间
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, num);// 把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
		DateFormat df = new SimpleDateFormat("yyyyMMdd");
			return df.format(date)+"";
	}
	/***
	 * 返回当日时间戳20151002
	 * 
	 * @return
	 */
	public static String submitDatetime() {
		Date date = new Date();
		DateFormat format = new SimpleDateFormat("yyyyMMdd");
		String time = format.format(date);
		long datetime = Long.parseLong(time);
		return datetime + "";
	}
	
	public static String getDateStr_ymd(int num){
		Date date = new Date();// 取时间
		Calendar calendar = new GregorianCalendar();
		calendar.setTime(date);
		calendar.add(Calendar.DATE, num);// 把日期往后增加一天.整数往后推,负数往前移动
		date = calendar.getTime(); // 这个时间就是日期往后推一天的结果
		DateFormat df = new SimpleDateFormat(YYYY_MM_DD);
			return df.format(date)+"";
	}
	public static String getYear(){
		Date date = new Date();// 取时间
		DateFormat df = new SimpleDateFormat("yyyy");
		return df.format(date)+"";
	}
	public static void main(String[] args) {
		System.out.println(getDateStr_ymd(-1));
	}
}